package com.example.backend.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.backend.domain.entity.RestBean;
import com.example.backend.domain.po.Customers;
import com.example.backend.service.ICustomersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dyy
 * @since 2025-05-27
 */
@RestController
@RequestMapping("/customers")
public class CustomersController {
    @Autowired
    private ICustomersService customersService;

    // 根据 ID 查询单个顾客
    @GetMapping("/{id}")
    public RestBean<Customers> getCustomerById(@PathVariable Integer id) {
        return RestBean.success(customersService.getById(id));
    }

    // 根据多个 ID 查询顾客
    @GetMapping("/batch")
    public RestBean<List<Customers>> getCustomersByIds(@RequestParam List<Integer> ids) {
        return RestBean.success(customersService.listByIds(ids));
    }

    // 根据条件查询顾客
    @GetMapping("/search")
    public RestBean<List<Customers>> searchCustomers(
            @RequestParam(required = false) String fullName,
            @RequestParam(required = false) Integer userId) {
        LambdaQueryWrapper<Customers> queryWrapper = new LambdaQueryWrapper<>();
        if (fullName != null && !fullName.isEmpty()) {
            queryWrapper.like(Customers::getFullName, fullName);
        }
        if (userId != null) {
            queryWrapper.eq(Customers::getUserId, userId);
        }
        return RestBean.success(customersService.list(queryWrapper));
    }

    // 分页查询顾客
    @GetMapping("/page")
    public RestBean<Page<Customers>> getCustomersByPage(
            @RequestParam Integer current,
            @RequestParam Integer size,
            @RequestParam(required = false) String fullName,
            @RequestParam(required = false) Integer userId) {
        Page<Customers> page = new Page<>(current, size);
        LambdaQueryWrapper<Customers> queryWrapper = new LambdaQueryWrapper<>();
        if (fullName != null && !fullName.isEmpty()) {
            queryWrapper.like(Customers::getFullName, fullName);
        }
        if (userId != null) {
            queryWrapper.eq(Customers::getUserId, userId);
        }
        return RestBean.success(customersService.page(page, queryWrapper));
    }

    // 新增顾客
    @PostMapping
    public RestBean<Boolean> addCustomer(@RequestBody Customers customer) {
        return RestBean.success(customersService.save(customer));
    }

    // 更新顾客信息
    @PutMapping("/{id}")
    public RestBean<Boolean> updateCustomer(
            @PathVariable Integer id,
            @RequestBody Customers customer) {
        customer.setId(id);
        return RestBean.success(customersService.updateById(customer));
    }

    // 删除顾客
    @DeleteMapping("/{id}")
    public RestBean<Boolean> deleteCustomer(@PathVariable Integer id) {
        return RestBean.success(customersService.removeById(id));
    }
}
